import { message } from 'ant-design-vue';
import { useAccountStore } from '@/stores/account.js';
import axios from 'axios';
function request(url, data) {
	axios.interceptors.request.use(
		function (config) {
			return config;
		},
		function (error) {
			return Promise.reject(error);
		}
	);

	axios.interceptors.response.use(
		function (response) {
			return Promise.resolve(response);
		},
		function (error) {
			return Promise.reject(error);
		}
	);

	return new Promise((resolve, reject) => {
		const account = useAccountStore();
		axios({
			headers: {
				'Content-Type': 'application/json;charset=UTF-8',
				token: sessionStorage.getItem('token')
			},
			method: 'POST',
			url: '/services' + url,
			data: data,
			timeout: 60000
		})
			.then((response) => {
				console.log(response);
				if (response.status === 200) {
					if (response.data.code === '200') {
						resolve(response.data.data);
					} else if (response.data.code === '500') {
					account.spinning = false;
					message.error(response.data.message);
					} else if (response.data.code === '401') {
					account.spinning = false;
					window.location.replace('/');
					}
				} else {
					account.spinning = false;
					message.error(response);
				}
			})
			.catch((err) => {
				account.spinning = false;
				reject(err);
			});
	});
}

export default request;
